package org.fao.geonet.services.param.branch;

import org.jdom.*;

import jeeves.constants.*;
import jeeves.interfaces.*;
import jeeves.resources.dbms.*;
import jeeves.server.*;
import jeeves.server.context.*;
import jeeves.utils.Util;

import org.fao.geonet.constants.*;

//=============================================================================

/**
 * Retrieves a particular user
 */

public class AreaCategory implements Service {
	// --------------------------------------------------------------------------
	// ---
	// --- Init
	// ---
	// --------------------------------------------------------------------------

	public void init(String appPath, ServiceConfig params) throws Exception {

	}

	// --------------------------------------------------------------------------
	// ---
	// --- Service
	// ---
	// --------------------------------------------------------------------------
	public Element exec(Element params, ServiceContext context)
			throws Exception {

		String id_organization = params.getChildText("id_organization", null);
		String id_branch = params.getChildText("id_branch", null);
		String id_area = params.getChildText("id_area", null);
		String id_category = params.getChildText("id_category", null);

		Dbms dbms = (Dbms) context.getResourceManager()
				.open(Geonet.Res.MAIN_DB);

		Element elEntityMaster = new Element(Geonet.Elem.RECORD);

		// Buscar solo las organizaciones con areas
		Element elDetail1 = new Element(Geonet.Elem.ORGANIZATIONS);
		elDetail1.addContent(dbms
				.select("SELECT DISTINCT O.id AS id_organization, O.name "
						+ "FROM organization O, organization_branch B, area A "
						+ "WHERE O.id = B.id_organization "
						+ "AND B.id = A.id_organization_branch "));

		Element elDetail2 = new Element(Geonet.Elem.BRANCHES);
		Element elDetail3 = new Element(Geonet.Elem.AREA);
		Element elDetail4 = new Element("relations");

		if (id_organization != null && !id_organization.equals("")) {
			elDetail2
					.addContent(dbms
							.select("SELECT DISTINCT B.id AS id_branch, B.name FROM organization_branch B, area A WHERE B.id = A.id_organization_branch AND id_organization = "
									+ id_organization));

			if (id_branch != null && !id_branch.equals("")) {
				elDetail3
						.addContent(dbms
								.select("SELECT A.id AS id_area, A.name FROM area A WHERE id_organization_branch = "
										+ id_branch));
				if (id_area != null && !id_area.equals("")) {
					if (id_category != null
							&& !id_category.equals("")
							&& params.getChildText("new_relation", null) != null
							&& params.getChildText("new_relation", null)
									.equals("true")) {
						try {
							dbms.execute("DELETE FROM area_category "
									+ "WHERE id_area=" + id_area
									+ " AND id_category = " + id_category);
							dbms
									.execute(
											"INSERT INTO area_category (id_area, id_category ) VALUES ( ?, ? )",
											new Integer(id_area), new Integer(
													id_category));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					elDetail4
							.addContent(dbms
									.select("SELECT A.id_area, A.id_category, B.name "
											+ "FROM area_category A, categories B "
											+ "WHERE A.id_category = B.id AND A.id_area = "
											+ id_area));

				}

			}
		}

		elEntityMaster.addContent(elDetail1);
		elEntityMaster.addContent(elDetail2);
		elEntityMaster.addContent(elDetail3);
		elEntityMaster.addContent(elDetail4);

		return elEntityMaster;
	}
}

// =============================================================================

